export default class ObjParser {
    parse(content: string) {
        const lines = content.split('\n');
        const vertices = [];
        const normals = [];
        const textureCoords = [];
        const faces = [];

        lines.forEach((line) => {
            const values = line.split(' ');
            const type = values[0];

            switch (type) {
                case 'v': // 顶点
                    vertices.push(
                        parseFloat(values[1]),
                        parseFloat(values[2]),
                        parseFloat(values[3])
                    );
                    break;

                case 'vt': // 纹理坐标
                    textureCoords.push(
                        parseFloat(values[1]),
                        parseFloat(values[2])
                    );
                    break;

                case 'vn': // 法线
                    normals.push(
                        parseFloat(values[1]),
                        parseFloat(values[2]),
                        parseFloat(values[3])
                    );
                    break;

                case 'f': // 面
                    const face = values.slice(1).map((vertex) => {
                        const indices = vertex.split('/').map((index) => parseInt(index));
                        return indices;
                    });
                    faces.push(face);
                    break;

                default:
                    break;
            }
        });

        // console.log('Vertices:', vertices);
        // console.log('Normals:', normals);
        // console.log('Texture Coords:', textureCoords);
        // console.log('Faces:', faces);

        return { vertices, normals, textureCoords, faces };
    }
}